package top.loveyumin1314.music.lottery;

import top.loveyumin1314.music.bean.LotteryBean;
import top.loveyumin1314.music.bean.UserActionBean;
import top.loveyumin1314.music.bean.UserListBean;
import top.loveyumin1314.music.cmd.CreateResponse;
import top.loveyumin1314.music.code.ActionCode;
import top.loveyumin1314.music.serviceimpl.BroadcastService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * author Kerry
 * date 2017/10/11
 * desp null
 */

public class LotteryServiceImpl implements LotteryService {

//    private LotteryThread lotteryThread;


    private synchronized LotteryBean prevAddLottoryUser(UserActionBean userActionBean) {
        if (IS_START_LOTTERY.get()) {
            return null;
        }
        String uuid;

        if (userActionBean.getActionId().equals(ActionCode.ADD_LOTTORY_MINE.actionId)) {
            uuid = userActionBean.getUserId();
        } else if (userActionBean.getActionId().equals(ActionCode.ADD_LOTTORY.actionId)) {
            uuid = UUID.randomUUID().toString();
        } else {
            return null;
        }
        LotteryBean lb = new LotteryBean();
        lb.setUuid(uuid);
        lb.setTime(System.currentTimeMillis());
        lb.setUserName(userActionBean.getData().toString());

        if (LOTTERY_USERS.containsKey(uuid)) {
            return null;
        }
        LOTTERY_USERS.put(uuid, lb);
        return lb;
    }


    @Override
    public Object addLotteryUser(UserActionBean userActionBean) throws IOException {
        LotteryBean isAddOk = prevAddLottoryUser(userActionBean);

        if (isAddOk == null) {
            //只通知添加那个
            userActionBean.setActionId(ActionCode.ADD_LOTTORY_FAILED.actionId);
            userActionBean.setActionDetail(ActionCode.ADD_LOTTORY_FAILED.actionDetail);
            return userActionBean;
        }
        //添加成功,通知所有人
        CreateResponse createResponse = CreateResponse.create().setUserActionBeanWithActionCode(userActionBean, ActionCode.ADD_LOTTORY_SUCCESS).setData(isAddOk);
        BroadcastService.getInstance().broadcast(createResponse);
        return null;
    }


    @Override
    public UserListBean<LotteryBean> getLotteryList(UserActionBean userActionBean) {

        UserListBean<LotteryBean> userListBean = new UserListBean<>();
        List<LotteryBean> values = new ArrayList<>(LOTTERY_USERS.values());
        userListBean.setUserId(userActionBean.getUserId());
        userActionBean.setUserName(userActionBean.getUserName());
        userListBean.setActionId(ActionCode.GET_LOTTORY_LIST.actionId);
        userListBean.setActionDetail(ActionCode.GET_LOTTORY_LIST.actionDetail);
        userListBean.setItemList(values);

        return userListBean;
    }

    @Override
    public Object startLottory(UserActionBean userActionBean) throws IOException {
        if (LOTTERY_USERS.isEmpty()) {
            userActionBean.setActionCode(ActionCode.LOTTORY_NO_USER);
            userActionBean.setTime(System.currentTimeMillis());
            return userActionBean;
        } else if (LOTTERY_USERS.size() == 1) {
            LotteryBean lb = null;
            for (LotteryBean lotteryBean : LOTTERY_USERS.values()) {
                lb = lotteryBean;
                break;
            }
            userActionBean.setActionCode(ActionCode.LOTTORY_LUCKLY);
            userActionBean.setData(lb);
            userActionBean.setTime(System.currentTimeMillis());
            BroadcastService.getInstance().broadcast(CreateResponse.create().setUserActionBean(userActionBean));
        } else {
            synchronized (this) {
                if (LotteryService.IS_START_LOTTERY.get()) {
                    userActionBean.setActionCode(ActionCode.LOTTORY_ING);
                    userActionBean.setTime(System.currentTimeMillis());
                    return userActionBean;
                }

              /*  if (lotteryThread != null) {
                    userActionBean.setActionCode(ActionCode.LOTTORY_ING);
                    userActionBean.setTime(System.currentTimeMillis());
                    return userActionBean;
                }*/

                LotteryService.IS_START_LOTTERY.set(true);

                userActionBean.setActionCode(ActionCode.B_START_NOTIFY);
                userActionBean.setTime(System.currentTimeMillis());
                userActionBean.setData(null);
                LotteryThread lotteryThread = new LotteryThread();
                new Thread(lotteryThread).start();
                BroadcastService.getInstance().broadcast(CreateResponse.create().setUserActionBean(userActionBean));
            }
        }
        return null;
    }


    @Override
    public Object stopLottory(UserActionBean userActionBean) throws IOException {

        synchronized (this) {

          /*  if (lotteryThread == null) {
                LotteryService.IS_START_LOTTERY.set(false);
                return null;
            }*/

            if (!LotteryService.IS_START_LOTTERY.get()) {
                return null;
            }

            LotteryService.IS_START_LOTTERY.set(false);
            LotteryThread.setUserActionBean(userActionBean);
//            lotteryThread.setUserActionBean(userActionBean);
        }
        return null;
    }

    @Override
    public Object removeLotteryUser(UserActionBean userActionBean) throws IOException {
        LotteryBean isRemove = prevRemoveLottoryUser(userActionBean);

        if (isRemove == null) {
            //只通知添加那个
            userActionBean.setActionCode(ActionCode.REMOVE_LOTTORY_FAILED);
            userActionBean.setTime(System.currentTimeMillis());
            return userActionBean;
        }
        CreateResponse createResponse = CreateResponse.create().setUserActionBeanWithActionCode(userActionBean, ActionCode.REMOVE_LOTTORY_SUCCESS).setData(isRemove);
        BroadcastService.getInstance().broadcast(createResponse);
        return null;

    }

    private synchronized LotteryBean prevRemoveLottoryUser(UserActionBean userActionBean) {
        if (IS_START_LOTTERY.get()) {
            return null;
        }
        String data = userActionBean.getData().toString();
        return LOTTERY_USERS.remove(data);
    }

}
